<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;

$param = false;
$mySql = '';
$moreJS = 1;

$sid	 = isset($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;
$view	 = isset($_G['gp_view']) ? strval($_G['gp_view']) : '';
$page	 = isset($_G['gp_page']) ? intval($_G['gp_page']) : 0;
$sort	 = isset($_G['gp_sort']) ? strval($_G['gp_sort']) : 'last';
$pnumber = isset($_G['gp_pnumber']) ? intval($_G['gp_pnumber']) : 0;

/*************LOAD MENU CATGORIES***************/
$shop_menu = shop_menu($sid);

if (!empty($shop_menu)){
	$isMenu = 1;

	switch ($view){
		case 'list':
			$view = 'list';
		break;
		default:
			$view = 'grid';
		break;
	}
}else{
	$isMenu = 0;

	switch ($view){
		case 'grid':
			$view = 'grid';
		break;
		default:
			$view = 'list';
		break;
	}

	if (empty($_G['shop']['param']))
		$param = false;
	else{
		$param = true;

		// Load param of this category
		$allParam = listAttr($sid);
		$totalParam = count($allParam);
	}
}

//////////////////////////////////////////////////////////////////////////////////////

$property = array();
$pArray = array();
$arrays = array();
$sArray = array();
$nArray = array();
$rArray = array();
$gp_param = '';

if ($pnumber){
	// Get all param
	for ($i = 0; $i < $pnumber; $i++){
		$property[] = $_G['gp_p' . $i];
	}

	// for url
	foreach ($property as $keys=>$values){
		if ($values != null){
			foreach ($values as $val){
				$gp_param .= '&amp;p' . $keys . '[]=' . str_replace(' ', '+', $val);
			}
		}
	}

	// remove key is null
	$property = array_filter($property);

	foreach ($property as $values){
		foreach ($values as $value){
			$arrays[] = $value;
		}
	}

	//////////////////////////////////////FOR PROPERTIES/////////////////////////////
	foreach ($arrays as $value){
		$n_array = explode('_', $value);
		foreach ($n_array as $value2){
			$sArray[$n_array[0]][] = $value2;
		}
	}

	foreach ($sArray as $value){
		$value = array_unique($value);
		$nArray[] = implode(chr(124), $value);
	}

	foreach ($nArray as $key=>$vals){
		$rArray[] = explode(chr(124), $vals);
	}
	/////////////////////////////////////////////////////////////////////////////////

	$pArray = array_filter($arrays);

	// Co tat ca 30 thuoc tinh
	$mySql .= searchAttr($pArray, $totalParam);
}

//////////////////////////////////////////////////////////////////////////////////////

$limit = $item_per_page;
$curpage = is_numeric($_G['gp_page']) ? intval($_G['gp_page']) : 1;
$start = ($curpage - 1) * $limit;
$count = 0;

$_G['gp_ajaxtarget'] = 'listproduct_display';

$xUrl = '';
$vUrl = '';
$sUrl = 'shop.php?mod=' . $mod . '&amp;sid=' . $sid . '&amp;pnumber=' . $pnumber . $gp_param; //echo $sUrl;

if ($sort)
	$xUrl = '&amp;sort=' . $sort;
if ($page)
	$xUrl = '&amp;page=' . $page;
if ($view){
	$xUrl = '&amp;view=' . $view;
	$vUrl = $sUrl . $xUrl;
}

$url = $sUrl . $xUrl;

$result = productList($sid, $mySql, $sort, $start, $limit, $count, true, true);
$pages = multi($count, $limit, $curpage, $url, 0, 10, false, false, 0);
//var_dump($result);
$have = count($result);

unset($_G['gp_ajaxtarget']);

/*********************SEO category***********************/
$navtitle = lang("shop/template", 'ket_qua_tim_kiem_trong');
$navtitle .= $_G['shop']['name'];
$metakeywords = $metakeywords = $navtitle;

switch ($sort){
	case 'pasc':
		$navtitle .= lang('shop/template', 'title_gia_thap_nhat');
	break;
	case 'pdesc':
		$navtitle .= lang('shop/template', 'title_gia_cao_nhat');
	break;
	case 'sale':
		$navtitle .= lang('shop/template', 'title_hang_giam_gia');
	break;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

include template('shop/filter_result');

unset($shop_menu);